package org.example.service;


import org.example.mapper.StudentMapper;
import org.example.mapper.ExperimentMapper;
import org.example.model.Student;
import org.example.model.Experiment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExperimentService {
    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private StudentMapper studentMapper;

    public Experiment getExperimentById(Long id) {
        return experimentMapper.getExperimentById(id);
    }

    public List<Student> getStudentsMissingExperiment(Long experimentId) {
        Experiment experiment = experimentMapper.getExperimentById(experimentId);
        if (experiment == null) {
            return new ArrayList<>();
        }

        List<Student> allStudents = studentMapper.getAllStudents();
        List<Student> missingStudents = new ArrayList<>();

        for (Student student : allStudents) {
            // 模拟检查学生是否提交实验的逻辑
            // 实际项目中，这里应该根据你的数据库设计进行查询
            boolean isSubmitted = false; // 默认为未提交
            if (!isSubmitted) {
                missingStudents.add(student);
            }
        }

        return missingStudents;
    }

    // 导出实验统计结果为JSON
    public String exportExperimentStatsToJson(Long experimentId) {
        Experiment experiment = experimentMapper.getExperimentById(experimentId);
        List<Student> missingStudents = getStudentsMissingExperiment(experimentId);

        String json = "{\n" +
                "  \"experiment\": {\n" +
                "    \"id\": " + experiment.getId() + ",\n" +
                "    \"name\": \"" + experiment.getName() + "\",\n" +
                "    \"courseName\": \"" + experiment.getCourseName() + "\",\n" +
                "    \"dueDate\": \"" + experiment.getDueDate() + "\",\n" +
                "    \"missingStudents\": [\n";

        for (int i = 0; i < missingStudents.size(); i++) {
            Student student = missingStudents.get(i);
            json += "      {\n" +
                    "        \"id\": " + student.getId() + ",\n" +
                    "        \"name\": \"" + student.getName() + "\",\n" +
                    "        \"className\": \"" + student.getClassName() + "\"\n";
            if (i < missingStudents.size() - 1) {
                json += "      },\n";
            } else {
                json += "      }\n";
            }
        }

        json += "    ]\n" +
                "  }\n" +
                "}";

        return json;
    }

    // 导出实验统计结果为CSV
    public String exportExperimentStatsToCsv(Long experimentId) {
        Experiment experiment = experimentMapper.getExperimentById(experimentId);
        List<Student> missingStudents = getStudentsMissingExperiment(experimentId);

        String csv = "Experiment ID,Experiment Name,Course Name,Due Date,Student ID,Student Name,Class Name\n";

        for (Student student : missingStudents) {
            csv += experiment.getId() + ",";
            csv += "\"" + experiment.getName() + "\",";
            csv += "\"" + experiment.getCourseName() + "\",";
            csv += "\"" + experiment.getDueDate() + "\",";
            csv += student.getId() + ",";
            csv += "\"" + student.getName() + "\",";
            csv += "\"" + student.getClassName() + "\"\n";
        }

        return csv;
    }
}